package com.aysafety.iotdataplatformserver.listener;

import com.alibaba.fastjson.JSON;
import com.aysafety.core.DBDriver.hbase.HBaseDriver;
import com.aysafety.core.common.DateUtils;
import com.aysafety.core.model.MicroseismModel;
import com.aysafety.iotdataplatformserver.dao.MicroseismDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

// 微震
@Component public class MicroseismListener {

    private final String type = "Microseism";

    private final String tableName = "MicroseismRealTime";

    private final String[] structure = new String[] {"Microseism"};

    private final String calTableName = "CalMicroseism";

    private final String[] calStructure = new String[] {"energyDeviation", "frequency"};

    private final String calWarningTableName = "CalWarningMicroseism";

    private final String[] calWarningStructure = new String[] {"warning"};
    @Autowired KsTreeDataListener ksTreeDataListener;
    @Autowired MicroseismDao microseismDao;
    private Logger logger = LoggerFactory.getLogger(ErrorListener.class);

    public void start(Map<String, Object> result) {
        try {
            logger.info("MicroseismListener.start,result=" + result);
            String clientId = result.get("clientId").toString();
            if (!result.get("dataType").toString().equals("histroy")) {
                Map<String, Object> data = JSON.parseObject(result.get("data").toString(), Map.class);
                MicroseismModel microseismData = this.transformData(data);
                this.save(microseismData, clientId);
            } else {
                List<Map<String, Object>> listDatas = JSON.parseObject(result.get("data").toString(), List.class);
                for (Map<String, Object> listData : listDatas) {
                    MicroseismModel microseismData = this.transformData(listData);
                    this.save(microseismData, clientId);
                }
            }

        } catch (IOException e) {
            logger.info("MicroseismListener:", e);
        }
    }

    public boolean save(MicroseismModel microseismData, String clientId) throws IOException {
        String timestamp = String.valueOf(microseismData.getDatetime().getTime());
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("tableId", microseismData.getEntId() + "." + "MicroseismRealTime");
        map.put("pk", "\'" + timestamp + "\'");
        map.put("clientId", "\'" + clientId + "\'");
        map.put("entId", "\'" + microseismData.getEntId() + "\'");
        map.put("area", "\'" + microseismData.getArea() + "\'");
        map.put("workingFace", "\'" + microseismData.getWorkingFace() + "\'");
        map.put("datetime", microseismData.getDatetime().getTime());
        map.put("x", microseismData.getX());
        map.put("y", microseismData.getY());
        map.put("z", microseismData.getZ());
        map.put("type", "\'" + microseismData.getType() + "\'");
        map.put("emergoa", microseismData.getEmergoa());
        map.put("poklad", microseismData.getPoklad());
        map.put("cause", "\'" + microseismData.getCause() + "\'");
        microseismDao.insertRealMicroseism(map);
        ksTreeDataListener.generateTreeData(microseismData.getEntId(), this.type, microseismData.getWorkingFace(), "",
            microseismData.getDatetime(), "");
        return true;
    }

    public MicroseismModel transformData(Map<String, Object> result) {
        MicroseismModel microseismModel = new MicroseismModel();
        if (null != result.get("area")) {
            microseismModel.setArea(result.get("area").toString());
        } else {
            microseismModel.setArea("无数据");
        }
        if (null != result.get("workingFace")) {
            microseismModel.setWorkingFace(result.get("workingFace").toString());
        } else {
            microseismModel.setWorkingFace("无数据");
        }
        microseismModel.setDatetime(DateUtils.fomatDateTime(Long.valueOf(result.get("datetime").toString())));
        microseismModel.setEntId(result.get("entId").toString());
        microseismModel.setX(Double.valueOf(result.get("x").toString()));
        microseismModel.setY(Double.valueOf(result.get("y").toString()));
        microseismModel.setZ(Double.valueOf(result.get("z").toString()));
        if (null != result.get("type")) {
            microseismModel.setType(result.get("type").toString());
        } else {
            microseismModel.setType("无数据");
        }
        if (null != result.get("emergoa")) {
            microseismModel.setEmergoa(Double.valueOf(result.get("emergoa").toString()));
        } else {
            microseismModel.setEmergoa(-1.0);
        }
        if (null != result.get("poklad")) {
            microseismModel.setPoklad(Double.valueOf(result.get("poklad").toString()));
        } else {
            microseismModel.setPoklad(-1.0);
        }
        if (null != result.get("cause")) {
            microseismModel.setCause(result.get("cause").toString());
        } else {
            microseismModel.setCause("无数据");
        }
        return microseismModel;
    }

    public boolean createTable() throws IOException {
        return HBaseDriver.createTable(this.tableName, this.structure);
    }

    public boolean createCalTable() throws IOException {
        return HBaseDriver.createTable(this.calTableName, this.calStructure);
    }

    public boolean createCalWarningTable() throws IOException {
        return HBaseDriver.createTable(this.calWarningTableName, this.calWarningStructure);
    }
}
